package com.neu.table;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import com.neu.pojo.Room;
import com.neu.service.impl.RoomService;

public class RoomTable extends AbstractTableModel{
	//fields
	private String[] colNames = {"","房间号","房间类型","可容纳人数","当前人数","物业负责人"};
	private Object[][] data;
	
	private RoomService roomService = RoomService.getInstance();
	
	//constructor
	public RoomTable() {
		ArrayList<Room> rooms = this.roomService.list();
		
		if(rooms != null&& rooms.size() != 0) {
			this.data = new Object[rooms.size()][colNames.length];
			for(int i=0;i<rooms.size();i++) {
				data[i][0] = new Boolean(false);
				data[i][1] = rooms.get(i).getLocation();
				data[i][2] = rooms.get(i).getKind();
				data[i][3] = rooms.get(i).getRoom();
				data[i][4] = rooms.get(i).getCurvalue();
				data[i][5] = rooms.get(i).getProperty().getName();
			}
		}else {
			this.data = new Object[0][0];
		}
	}
	
	//返回结果行数
	@Override
	public int getRowCount() {
		return this.data.length;
	}

	//返回结果列数
	@Override
	public int getColumnCount() {
		return this.colNames.length;
	}
	
	//获得对应列名
	@Override
	public String getColumnName(int column) {
		return this.colNames[column];
	}

	//每列对应的类型
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return data[0][columnIndex].getClass();
	}
	
	//返回哪行哪列对应的数据
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex];
	}
	
	/**
	 * 哪列不能编辑
	 * 返回false 不可编辑
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {		//编号不能编辑
			return true;
		} else
			return false;
	}
	
	//修改表格中的数据
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		data[rowIndex][columnIndex] = aValue;
		/*修改表格数据后，显示改后结果*/
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	@Override
	public void fireTableRowsDeleted(int firstRow, int lastRow) {
		super.fireTableRowsDeleted(firstRow, lastRow);
	}
	
//	public int delete() {
//		int cnt=0;//表示要删除员工的个数
//        for (int i=0;i<getRowCount();i++) {
//			if ((boolean)data[i][0]) {
//				System.out.println(workerService.delete(new Worker((String) data[i][1])));
//				cnt+=1;
//			}
//        }
//        if(cnt == 0) {
//        	return cnt;
//        }
//        //文件中数据已经更改
//        fireTableDataChanged();
//	    Object[][] date2=new Object[getRowCount()-cnt][7];
//	    int j=0;
//	    for (int i=0;i<getRowCount();i++) {
//			if (getValueAt(i,0).equals(false)) {//如果没选中
//				date2[j][0]=new Boolean(false);
//				date2[j][1]=data[i][1];
//				date2[j][2]=data[i][2];
//				date2[j][3]=data[i][3];
//				date2[j][4]=data[i][4];
//				date2[j][5]=data[i][5];
//				date2[j][6]=data[i][6];
//				j++;
//			}	
//		}
//	    data=new Object[getRowCount()-cnt][7];
//	    data=date2;
//	    return cnt;
//	}
}
